<?php
/**
 * @Author: Ron Chen
 * @Date:   2017-03-23 22:20:02
 * @Last Modified by:   Ron Chen
 * @Last Modified time: 2017-09-07 22:40:44
 */
namespace app\controllers;

use yii\web\Controller;
use app\models\InsuranceProject;
use app\models\InsurancePlan;
use app\models\InsurancePrice;
use app\models\InsuranceBenifit;

class IndexController extends Controller {

	public $title   = '';
	public $desc    = '';
	public $layout  = "insurance";
	public function init(){
		$this->layout = "insurance";
		$this->title  = "趣寻网";
		$this->desc   = "趣寻网-打造互联网保险小生态";
		parent::init();
	}
	/**
	 * @name 产品列表页面
	 */
	public function actionList(){
		$code = trim(\Yii::$app->request->get('code',''));
		$cbk_project = null;
		
		if ($code) {
			if(@stripos($code, "_")){
				$code_arr = explode('_', $code);
				$code_str = $code_arr ? $code_arr[0] : "";
			}else{
				$code_str = $code;
			}
			$cbk_project = InsuranceProject::checkProductIsExist($code_str);
		}
		if (empty($cbk_project) || empty($code)) {
			$code = '';
		}
		$project = InsurancePlan::getInsurancePlanList($code);
		return $this->render ('list',[
			'project' => $project 
		]);
	}

	/**
	 * @name 产品详情页
	 * AppMainService::getProductUserVerifySet、common\models\product\ProductVerfication
	 */
	public function actionDetail(){
		$this->layout = "insurance-detail";
		$code = trim(\Yii::$app->request->get('id','FFAXLX'));
		$source = trim(\Yii::$app->request->get('source',''));
		
		$plan = InsurancePlan::find()->where([
			'plan_code' => $code
		])->orderBy ('plan_order DESC')->limit(1)->one();

		if ($plan) {
			$project = InsuranceProject::find()->where ([
				'project_code' => $plan->project_code
			])->orderBy ('project_order DESC')->limit(1)->one();
		}

		if (empty($plan) || empty($project)){
			$this->redirect(['/index/list']);
		}

		// 获取服务条款
		$benifit = InsuranceBenifit::find()->where(['plan_code' => $code])->all();
		$benifit_arr = [];
		if (empty($benifit)) {
			$benifit_arr = [[
				"name" => '自驾车意外身故及伤残',
				"price"=> '0.00',
			],[
				"name" => '仅承保境外旅行',
				"price"=> '0.00'
			]];
		}else{
			foreach ($benifit as $row) {
				$benifit_arr[] = [
					"name" => $row->benifit_name,
					"price" => $row->benifit_amount
				];
			}
		}
		// 如果产品是<<留学无忧>> --
		// 显示计划以及周期
		$project_price = [];
		// $project_price_info = InsurancePrice::getInsurancePriceByCode($code);
		$project_price_info = InsurancePrice::getInsurancePriceByPlan($code);
		if ($project_price_info) {
			foreach ($project_price_info as $item) {
				if (empty($item['start_age'])) {
					$project_price[0][] = [
						'id'=> $item['id'],
						'plan_name' => '12-40周岁',
						'price_time'=> $item['price_name_time'],
						'price_day'=> $item['price_day'],
						'price_amount'=> $item['price_amount'],
						'price_currency'=> $item['price_currency'],
					];
				}else{
					$project_price[$item['start_age']][] = [
						'id'=> $item['id'],
						'plan_name' => $item['price_name_age'],
						'price_time'=> $item['price_name_time'],
						'price_day'=> $item['price_day'],
						'price_amount'=> $item['price_amount'],
						'price_currency'=> $item['price_currency'],
					];
				}
			}
		}

		return $this->render ( 'detail', [ 
			'project' => $project,
			'plan' => $plan,
			'project_price' => $project_price,
			'benifit' => $benifit_arr,
			'source' => $source,
		]);
	}
}
	